import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
// import {getMerchartMenu} from '@/utils/api'
import http from "@/utils/http";
Vue.use(Vuex);

const initState = () => {
  return {
    regisMessTag: false, //注册提示开关
    regisMessTit: "",
    regisMessBtn: "",
    regisMessCommand: 0,

    liveTag: true, //状态保持开关
    menuTag: true, //左侧菜单开关
    custom: false, //客服弹窗状态
    gongdan: false, //工单弹窗状态
    testMode: false, // test模式弹窗
    testLine: false, // test状态开关
    path: "0", // 主菜单 path
    checkedMenuItem: {
      index: "1-0",
      children: [],
    }, // 子菜单path
    showChildMenu: false, // 是否展示右侧子菜单
    menuType: 1, // 1--商户面板  2--企业面板 3--个人面板
    globalSearch: false,
    appId: "",
    merchantMenu: [
      // { title: "工作台", index: "1", icon: 'el-icon-gongzuotai' },
      // {
      //   title: "商户",
      //   index: "2",
      //   icon: 'el-icon-shanghu',
      //   children: [
      //     { title: "子商户概览", index: "2-1" },
      //     { title: "子商户交易查询", index: "2-2" },
      //     { title: "子商户管理", index: "2-3" },
      //     { title: "创建子商户", index: "2-4" },
      //   ],
      // },
      // {
      //   title: "交易",
      //   icon: 'el-icon-jiaoyi',
      //   index: "3",
      //   children: [
      //     { title: "交易汇总", index: "3-1" },
      //     { title: "交易流水", index: "3-2" },
      //     { title: "业务订单", index: "3-3" },
      //     { title: "退款审核", index: "3-4" },
      //   ],
      // },
      // {
      //   title: "对账",
      //   index: "4",
      //   icon: 'el-icon-duizhang',
      //   children: [
      //     { title: "对账汇总", index: "4-1" },
      //     { title: "差错管理", index: "4-2" },
      //   ],
      // },
      // {
      //   title: "分账",
      //   index: "5",
      //   icon: 'el-icon-fenzhang',
      //   children: [
      //     { title: "分账汇总", index: "5-1" },
      //     { title: "分账规则", index: "5-2" },
      //     { title: "应付分账明细", index: "5-3" },
      //     { title: "应收分账明细", index: "5-4" },
      //     { title: "创建分账规则", index: "5-5" },
      //   ],
      // },
      // {
      //   title: "账户",
      //   index: "6",
      //   icon: 'el-icon-zhanghu',
      //   children: [
      //     { title: "我的钱包", index: "6-1" },
      //     { title: "充值记录", index: "6-2" },
      //     { title: "转账记录", index: "6-3" },
      //     { title: "提现记录", index: "6-4" },
      //     { title: "余额转账", index: "6-5" },
      //     { title: "提现", index: "6-6" },
      //   ],
      // },
      // {
      //   title: "用户",
      //   index: "7",
      //   icon: 'el-icon-yonghu',
      // },
      // {
      //   title: "优惠券",
      //   index: "8",
      //   icon: 'el-icon-youhuiquan',
      // },
      // {
      //   title: "设置",
      //   icon: 'el-icon-shezhi',
      //   index: "9",
      //   children: [
      //     { title: "成员管理", index: "9-1" },
      //     { title: "支付渠道", index: "9-2" },
      //     { title: "收款账户", index: "9-3" },
      //     { title: "退款审核开关", index: "9-4" },
      //     { title: "webhooks", index: "9-5" },
      //     { title: "联调工具", index: "9-6" },
      //   ],
      // },
    ],
    qiyeMenu: [
      // { title: "账户一览", index: "1" },
      // { title: "公司信息", index: "2" },
      // { title: "开发参数", index: "3" },
      // { title: "订单中心", index: "4" },
      // { title: "发票管理", index: "5" },
      // { title: "角色权限", index: "6" },
      // { title: "成员管理", index: "7" },
      // { title: "余额明细", index: "8" },
      // { title: "操作日志", index: "9" },
      // { title: "价格方案", index: "10" },
    ],
    gerenMenu: [
      { name: "账号信息", index: "0", children: [], url: "/geren/userInfo" },
      { name: "修改密码", index: "1", children: [], url: "/geren/password" },
      { name: "我的工单", index: "2", children: [], url: "/geren/workOrder" },
      { name: "操作日志", index: "3", children: [], url: "/geren/ULog" },
    ],
    treeList: [],
    buttonList: [],
    appList: [],
    mealList: [],
    channelList: [],
    channelListType: [],
    msgCount: 0,
  };
};

const deepEach = (arr, i) => {
  return arr.map((val, i_) => ({
    ...val,
    index: i + i_,
    children:
      val.children && val.children.length
        ? deepEach(val.children, i + i_ + "-")
        : [],
  }));
};
// console.log(http, '----------');
export default new Vuex.Store({
  plugins: [
    createPersistedState({
      storage: window.localStorage,
    }),
  ],
  namespaced: true,
  state: initState(),
  getters: {
    menuList(state) {
      if (state.menuType === 2) {
        return state.qiyeMenu;
      }
      if (state.menuType === 3) {
        return state.gerenMenu;
      }
      return state.merchantMenu;
    },
  },
  mutations: {
    changeRegisText(state, payload) {
      state.regisMessTit = payload.tit;
      state.regisMessBtn = payload.btn;
      state.regisMessCommand = payload.command;
    },
    changeRegisMess(state, payload) {
      state.regisMessTag = payload;
    },
    changeMenu(state, payload) {
      state.menuTag = payload;
    },
    changeLive(state, payload) {
      // console.log(payload)
      state.liveTag = payload;
    },
    changeTestMode(state, payload) {
      state.testMode = payload;
    },
    changeTestLine(state, payload) {
      state.testLine = payload;
    },
    changeCustom(state, payload) {
      state.custom = payload;
    },
    changeGongdan(state, payload) {
      state.gongdan = payload;
    },
    changeAppId(state, payload) {
      state.appId = payload;
    },
    changeMsgCount(state, payload) {
      state.msgCount = payload;
    },
    changeMenuType(state, payload) {
      state.menuType = payload;
    },
    changeGlobalSearch(state, payload) {
      state.globalSearch = payload;
    },
    changePath(state, payload) {
      state.path = payload;
    },
    changeCheckedMenuItem(state, payload) {
      state.checkedMenuItem = {
        ...state.checkedMenuItem,
        ...payload,
      };
    },
    changeShowChildMenu(state, payload) {
      state.showChildMenu = payload;
    },
    resetState(state, payload = initState()) {
      for (let key in payload) {
        state[key] = payload[key];
      }
    },
  },
  actions: {
    changeLive({ commit }, p) {
      commit("changeLive", {
        liveTag: p,
      });
    },
    getChannelList({ commit }) {
      http.get("/api/dict/channelList").then((res) => {
        if (res.code === "0000") {
          var a = [{ label: "所有", value: "" }];
          var b = a.concat(res.data);
          commit("resetState", {
            channelList: b,
          });
        }
      });
    },
    getChannelListType({ commit }) {
      http.get("/api/dict/payTypeList").then((res) => {
        if (res.code === "0000") {
          var a = { label: "所有", value: "" };
          var b = res.data;
          let keyArr = Object.keys(b);
          keyArr.forEach((key) => {
            b[key].unshift({ label: "所有", value: "" });
          });
          // console.log(b)
          commit("resetState", {
            channelListType: b,
          });
        }
      });
    },
    getButton({ commit }) {
      http.get("/menu/getSpecialOperationButton").then((res) => {
        if (res.code === "0000") {
          commit("resetState", {
            buttonList: res.data,
          });
        }
      });
    },
    getAppList({ commit }) {
      http.get("/api/app/get").then((res) => {
        if (res.code === "0000") {
          commit("resetState", {
            appList: res.data,
          });
        }
      });
    },
    async getMealList({ commit }) {
      return http.get("/sys/meal/listMeal").then((res) => {
        if (res.code === "0000") {
          commit("resetState", {
            mealList: res.data,
          });
          // console.log("1-3");
        }
      });
    },
    async getMenu({ commit }) {
      return http
        .get("/menu/listMenuByUser", {
          params: {
            // systemType: 'company'
            systemType: "merchant",
          },
        })
        .then((res) => {
          if (res.code === "0000") {
            //console.log(res.data);
            commit("resetState", {
              merchantMenu: deepEach(res.data, ""),
            });
            // console.log("1-1");
          }
        });
    },
    async getMenuQiye({ commit }) {
      return http
        .get("/menu/listMenuByUser", {
          params: {
            systemType: "company",
            // systemType: 'merchant'
          },
        })
        .then((res) => {
          if (res.code === "0000") {
            commit("resetState", {
              qiyeMenu: res.data.map((item, i) => ({
                ...item,
                index: "" + i,
              })),
            });
            // console.log("1-2");
          }
        });
    },
    getTree({ commit }) {
      http.get("/api/merchant/memberbase/getTree").then((res) => {
        if (res.code === "0000") {
          const deepArr = (arr) => {
            return arr.map((item) => ({
              label: item.name,
              value: item.id,
              children: item.children.length ? deepArr(item.children) : null,
            }));
          };
          commit("resetState", {
            treeList: deepArr(res.data),
          });
        }
      });
    },
  },
});
